package com.sjgame.base.controller;

import com.sjgame.base.common.PageByBootStarp;
import com.sjgame.base.entity.Resource;
import com.sjgame.base.service.IResourceService;
import com.sjgame.model.entity.Log;
import com.sjgame.model.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

/**
 * create by zcl
 */
@Controller
public class IndexController extends BaseController {
    @Autowired
    private IResourceService resourceService;
    @Autowired
    private LogService logService;
    @RequestMapping(value = {"/", "/index"})
    public String index(ModelMap map) {
        List<Resource> list = resourceService.findAllByParentId(null);
        if(list!=null && list.size()>0){
            for (Resource r:list) {
                if(r.getType()==1){
                    r.setChilds(null);
                    continue;
                }
                if(r.getChilds()!=null && r.getChilds().size()>0){
                    dealResource(r);
                }
            }

        }
            map.put("treeMenu", list);


        return "index";
    }



    @RequestMapping(value = {"/", "/welcome"})
    public String welcome(ModelMap map) {
//       String account = getUser().getUserName();
//        Sort t  = new Sort(Sort.Direction.DESC,"createTime");
//        Page<Log> page = logService.findAll(new Specification<Log>() {
//            @Override
//            public Predicate toPredicate(Root<Log> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
//                Path<String> namepath = root.get("userName");
//                List<Predicate> list = new ArrayList<Predicate>();
//                if(account!=null&&!"".equals(account)) {
//                    list.add(cb.like(namepath,"%"+account+"%"));
//                }
//
//                Predicate[] p = new Predicate[list.size()];
//                return cb.and(list.toArray(p));
//            }
//        },getPageRequest(t));
//        map.put("pageInfo", page);

        return "welcome";
    }

    @RequestMapping(value = {"/", "/welcomeindex"})
    @ResponseBody
    public PageByBootStarp welcomeindex(ModelMap map) {


       String account = getUser().getUserName();
        Sort t  = new Sort(Sort.Direction.DESC,"createTime");
        Page<Log> page = logService.findAll(new Specification<Log>() {
            @Override
            public Predicate toPredicate(Root<Log> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<String> namepath = root.get("userName");
                List<Predicate> list = new ArrayList<Predicate>();
                if(account!=null&&!"".equals(account)) {
                    list.add(cb.like(namepath,"%"+account+"%"));
                }

                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },getPageRequest(t));


      // map.put("pageInfo", page);
        //按照要求定义一个HashMap作为返回json的格式

//        HashMap<String, Object> json=new HashMap<String, Object>();
//        json.put("total",page.getTotalElements());
//        json.put("rows",page.getContent());
        return new PageByBootStarp().formatResult(page);
    }

    private void  dealResource(Resource r){
        if(r.getChilds()!=null && r.getChilds().size()>0){
            for(Resource r2:r.getChilds()){
                if(r2.getType()==1){
                    r2.setChilds(null);
                    continue;
                }
                if(r2.getChilds()!=null && r2.getChilds().size()>0){
                    dealResource(r2);
                }
            }
        }
    }
}
